#include "timesettingwid.h"

TimeSettingWid::TimeSettingWid(QWidget *parent) :
    QWidget(parent)
{
    m_form.setupUi(this);
    this->setMinimumSize(300, 60);

    m_hour = m_min = m_sec = 0;
    m_user = m_time = 0;
    this->makeConnections();
}

void TimeSettingWid::makeConnections()
{
    QObject::connect(m_form.spinBox, SIGNAL(editingFinished()), this, SLOT(HourHandle()));
    QObject::connect(m_form.spinBox_2, SIGNAL(editingFinished()), this, SLOT(MinHandle()));
    QObject::connect(m_form.spinBox_3, SIGNAL(editingFinished()), this, SLOT(SecHandle()));
    QObject::connect(m_form.spinBox_4, SIGNAL(editingFinished()), this, SLOT(UserHandle()));
    QObject::connect(m_form.spinBox_5, SIGNAL(editingFinished()), this, SLOT(TimeHandle()));
}

void TimeSettingWid::setTimeSetting(int duration, int user, int time)
{
	int hour = duration / 3600;
	int min = (duration - hour * 3600) / 60;
	int sec = duration - hour * 3600 - min * 60;

	m_hour = hour;
	m_min = min;
	m_sec = sec;
	m_user = user;
	m_time = time;
	
	m_form.spinBox->setValue(hour);
	m_form.spinBox_2->setValue(min);
	m_form.spinBox_3->setValue(sec);
	m_form.spinBox_4->setValue(user);
	m_form.spinBox_5->setValue(time);
}

void TimeSettingWid::HourHandle()
{
    QSpinBox *sender = (QSpinBox *)this->sender();
    int hour = sender->value();
    if(hour != m_hour){
        m_hour = hour;
        emit durationChanged(m_hour * 3600 + m_min * 60 + m_sec);
    }
}

void TimeSettingWid::MinHandle()
{
    QSpinBox *sender = (QSpinBox *)this->sender();
    int min = sender->value();
    if(min != m_min){
        m_min = min;
        emit durationChanged(m_hour * 3600 + m_min * 60 + m_sec);
    }
}

void TimeSettingWid::SecHandle()
{
    QSpinBox *sender = (QSpinBox *)this->sender();
    int sec = sender->value();
    if(sec != m_sec){
        m_sec = sec;
        emit durationChanged(m_hour * 3600 + m_min * 60 + m_sec);
    }
}

void TimeSettingWid::UserHandle()
{
    QSpinBox *sender = (QSpinBox *)this->sender();
    int user = sender->value();
    if(user != m_user){
        m_user = user;
        emit userChanged(m_user);
    }
}

void TimeSettingWid::TimeHandle()
{
    QSpinBox *sender = (QSpinBox *)this->sender();
    int time = sender->value();
    if(time != m_time){
        m_time = time;
        emit timeChanged(m_time);
    }
}

